﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FtpWorkerRole.FtpServer;
using System.IO;

namespace FtpWorkerRole.FtpCommand
{
    public class CwdCommand : CommandBase
    {
        public CwdCommand(FtpSocketHandler handler)
            : base(handler)
        {
            commandName = "CWD";
        }

        public override void execute(string value)
        {
            if (!handler.Login)
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(530, "Please Login!"));
                return;
            }
            value = value.Trim().Replace('/', '\\');
            if (value == "")
            {
                value = handler.currentDirectory;
            }
            else
            {
                value = Path.Combine(handler.currentDirectory, value);
            }

            if (!handler.fileSystem.isDirectoryExist(value))
            {
                Utils.sendMessage(handler.getSocket(), formatMessage(550, "Directory not found"));
                return;
            }
            handler.currentDirectory = value;
            Utils.sendMessage(handler.getSocket(), formatMessage(250, "Directory successfully changed."));
        }
    }
}
